const http = require("../../../utils/http");

// pages/index/work/quality.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navindex: 0,
    pageName: null,
    show1: true,
    show: true,
    btnindex: 1,
    checked: false,
    arrayindex: 0,
    projectData: '',  //质检人及项目数据
    workOk: '', //完工时提交的数据
    technician: '', //选择服务技师
    fuwumolid: false, //选择技师弹窗
    molideJishi: '',  //技师弹窗的临时数据
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    http.postRequest('/mini/v1/work/staff_project',{work_number: options.work_number, work_id: options.work_id},res=> {
      let workOk = {};
      workOk.work_number = options.work_number;
      workOk.work_id = options.work_id;
      workOk.project_info = [];
      for(let i in res.data.work_project_data) {
        workOk.project_info[i] = {};
        workOk.project_info[i].technician_ids = JSON.parse(JSON.stringify(res.data.work_project_data[i].technician_ids));
        workOk.project_info[i].staff_data = JSON.parse(JSON.stringify(res.data.staff_data));
        workOk.project_info[i].qc_remark = '';
        workOk.project_info[i].qc_pass = 1;
        workOk.project_info[i].index = 0;
        workOk.project_info[i].name = res.data.work_project_data[i].project.name;
        workOk.project_info[i].work_project_id = res.data.work_project_data[i].project.id;
        workOk.project_info[i].selectChek = 0;
        for(let z in res.data.staff_data) {
          for(let a in workOk.project_info[i].technician_ids) {
            if(workOk.project_info[i].technician_ids[a] == res.data.staff_data[z].id) {
              workOk.project_info[i].staff_data[z].check = true;
              workOk.project_info[i].selectChek++;
            }
          }
        }
      }
      this.setData({
        workOk: workOk,
        projectData: res.data
      })
    })
  },
  navindex(e) {
    let index = e.currentTarget.dataset.index;
    this.setData({
      navindex: index
    })
  },
  checked() {
    let checked = this.data.checked
    checked = !checked
    this.setData({
      checked: checked
    })
  },
  // 合格
  btn(e) {
    let workOk = this.data.workOk;
    workOk.project_info[e.currentTarget.dataset.index].qc_pass = 2;
    this.setData({
      workOk: workOk
    })
  },
  // 不合格
  btn1(e) {
    let workOk = this.data.workOk;
    workOk.project_info[e.currentTarget.dataset.index].qc_pass = 1;
    this.setData({
      workOk: workOk
    })
  },
  //质检人
  bindPickerChange(e) {
    let workOk = this.data.workOk;
    workOk.project_info[e.target.dataset.index].qc_user_id = JSON.parse(JSON.stringify(this.data.projectData.staff_data[e.detail.value].id));
    workOk.project_info[e.target.dataset.index].index = e.detail.value;
    this.setData({
      workOk: workOk
    })
  },
  // 弹出技师弹窗
  sershow(e) {
    let workOk = this.data.workOk;
    let molideJishi = {};
    molideJishi.arr =JSON.parse(JSON.stringify(workOk.project_info[e.currentTarget.dataset.index].staff_data));
    molideJishi.selectChek = 0;
    molideJishi.index = e.currentTarget.dataset.index;
    for(let i in molideJishi.arr) {
      if(molideJishi.arr[i].check) {
        molideJishi.selectChek++;
      }
    }
    this.setData({
      fuwumolid: true,
      molideJishi: molideJishi
    })
  },
  // 技师弹窗的勾选
  jishicheckbox(e) {
    let molideJishi = this.data.molideJishi;
    molideJishi.arr[e.currentTarget.dataset.index].check = !molideJishi.arr[e.currentTarget.dataset.index].check;
    if(molideJishi.arr[e.currentTarget.dataset.index].check) {
      molideJishi.selectChek++;
    } else {
      molideJishi.selectChek--;
    }
    this.setData({
      molideJishi: molideJishi
    })
  },
  // 确定已勾选的技师
  jishiOk() {
    let molideJishi = this.data.molideJishi;
    let workOk = this.data.workOk;
    workOk.project_info[molideJishi.index].technician_ids = [];
    workOk.project_info[molideJishi.index].staff_data = JSON.parse(JSON.stringify(molideJishi.arr));
    for(let i in molideJishi.arr) {
      if(molideJishi.arr[i].check) {
        workOk.project_info[molideJishi.index].technician_ids.push(molideJishi.arr[i].id);
      }
    }
    this.setData({
      workOk: workOk,
      fuwumolid: false
    })
  },
  // 关闭选择技师弹窗
  fuwuhide(){
    this.setData({
      fuwumolid: false
    })
  },
  // 工单完工
  finish() {
    http.postRequest('/mini/v1/work/finish',this.data.workOk,res=> {
      if(res.code==0) {
        wx.navigateBack({
          delta: 2
        })
      }
    })
  },
  show() {
    this.setData({
      show: false
    })
  },
  hide() {
    this.setData({
      show: true
    })
  }
})